/**

 @Name：主入口
 @Author：GROW

 */

layui.extend({
    //配置文件
    setter: 'javascript/config',
    //核心模块
    admin: 'javascript/admin',
    //视图模块
    view: 'javascript/view'
}).define(['setter', 'admin'], function(exports) {
    var setter = layui.setter,
        element = layui.element,
        admin = layui.admin,
        view = layui.view,
        baseDir = layui.cache.base,

        //根据路由渲染页面
        renderPage = function() {
            var router = layui.router(),
                path = router.path,
                pathURL = admin.correctRouter(router.path.join('/'));

            //默认读取主页
            if (!path.length)
                path = [''];

            //如果最后一项为空字符，则读取默认文件
            if (path[path.length - 1] === '') {
                path[path.length - 1] = setter.entry;
            }

            //重置状态
            var reset = function(type) {
                if (renderPage.haveInit) {
                    $('.layui-layer').each(function() {
                        var othis = $(this),
                            index = othis.attr('times');
                        if (!othis.hasClass('layui-layim')) {
                            layer.close(index);
                        }
                    });
                }
                renderPage.haveInit = true;

                $(APP_BODY).scrollTop(0);
            };

            //请求视图渲染
            view().render(path.join('/')).done(function() {
                layui.use('common', layui.cache.callback.common);
                $win.on('resize', layui.data.resize);
                element.render('breadcrumb', 'breadcrumb');
            });

            reset();
        },

        //入口页面
        entryPage = function(fn) {
            var router = layui.router(),
                container = view('LAY_app'),
                pathURL = admin.correctRouter(router.path.join('/'));

            //将模块根路径设置为 controller 目录
            layui.config({
                base: baseDir + 'javascript/'
            });

            if (pathURL === '/user/login') { //独立页面
                container.render(router.path.join('/')).done(function() {
                    admin.pageType = 'alone';
                });
            } else { //后台框架页面
                //渲染后台结构
                if (admin.pageType === 'console') { //后台主体页
                    renderPage();
                } else { //初始控制台结构
                    container.render('layout').done(function() {
                        renderPage();
                        layui.element.render();

                        if (admin.screen() < 2) {
                            admin.sideFlexible();
                        }
                        admin.pageType = 'console';
                    });
                }

            }
        },

        APP_BODY = '#LAY_app_body',
        $ = layui.$,
        $win = $(window);

    //初始主体结构
    layui.link(
        baseDir + 'style/admin.css',
        function() {
            entryPage()
        }, 'layuiAdmin'
    );

    //监听Hash改变
    window.onhashchange = function() {
        entryPage();
        layui.event.call(this, setter.MOD_NAME, 'hash({*})', layui.router());
    };

    //对外输出
    exports('index', {
        render: renderPage
    });
});